package com.hnxxxy.sysmgr.controller;

import com.hnxxxy.common.AjaxRtnEntity.AjaxRtnEntity;
import com.hnxxxy.common.annotation.LogAnno;
import com.hnxxxy.sysmgr.pojo.*;
import com.hnxxxy.sysmgr.service.SysService;
import com.hnxxxy.sysmgr.utils.WebUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.UUID;

/**
 * @CLASSDESCRIPER:
 * @AUTHOR:suhui
 * @PHONE:13319575368
 * @EMIAL:13319575368@163.COM
 * @DATE: 2020/3/31
 * @PARAMDESCRIPER:
 * @OUTPARAMDESCRIPER:
 */
@RequestMapping("/sys")
@Controller
@Slf4j
public class SysController {
    
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SysService service;


    @PostMapping("/toLogin")
    @ResponseBody
    @LogAnno(description = "用户登录",descriptiontype = "login")
    public AjaxRtnEntity toLogin(@RequestBody Staff obj, ServletResponse response){
        AjaxRtnEntity rtnEntity;
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(obj.getLoginname(),obj.getPwd());
        try {
            subject.login(token);
      /*    String tokenBack = jwtUtil.generateToken(obj.getLoginname());
            ((HttpServletResponse)response).setHeader(jwtUtil.getHeader(), tokenBack);
            log.info("-->生产token："+tokenBack);*/
       rtnEntity=new AjaxRtnEntity(1,"登录成功",null);
        } catch (UnknownAccountException e) {
            rtnEntity=new AjaxRtnEntity(0,"用户信息不正确请重新输入",null);
        }catch (IncorrectCredentialsException e) {
            rtnEntity=new AjaxRtnEntity(0,"用户信息不正确请重新输入",null);
        }
        return rtnEntity;
    }

    @GetMapping("/main")
    @RequiresPermissions("1")
    @LogAnno(description = "请求主界面")
    public String main(Model model){
        //获取系统配置信息
        Project project=service.getProjectInfo();
        model.addAttribute("data",project);
        return "main";
    }

    @PostMapping("/getStaffCollectionInfo")
    @ResponseBody
    @LogAnno(description = "获取用户集合对象")
    public StaffCollection getStaffCollectionInfo(HttpSession session){
     /*   Staff staff = (Staff) session.getAttribute("staff");*/
        Subject subject= SecurityUtils.getSubject();
        Staff staff= (Staff) subject.getPrincipal();
       return service.getStaffCollectionInfo(staff);
    }

    public static void main(String args[]){
        String salt=UUID.randomUUID().toString();
        System.out.println(salt);
        int hashIterations=1000;
        Md5Hash md4=new Md5Hash("88888888",salt,1000);
        System.out.println(md4.toBase64());
    }

    @RequestMapping("/loginOut")
    @LogAnno(description = "用户退出登录",descriptiontype = "loginOut")
    public String LoginOut(){
        Subject subject = SecurityUtils.getSubject();
        Staff staff = (Staff) subject.getPrincipal();
        final String loginname = staff.getLoginname();
        redisTemplate.delete("Deque:" + loginname);
        subject.logout();
        return "redirect:/";
    }

    @RequestMapping("/getSelectList")
    @ResponseBody
    public AjaxRtnEntity getSelectList(@RequestBody  Dict dict){
        List<Dict> dictList= service.getSelectList(dict.getGroupid());
        AjaxRtnEntity ajaxRtnEntity=new AjaxRtnEntity(1,"",dictList);
        return ajaxRtnEntity;
    }


    @RequestMapping("/getCacheInfo")
    @ResponseBody
    public List<CacheTableObj> getCacheInfo(){
        return service.getSysCacheInfo();
    }
}

